package com.jereh.topnews.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.jereh.topnews.entity.Channel;
import com.jereh.topnews.entity.Content;
import com.jereh.topnews.entity.ImageUrl;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/28.
 */
public class ContentDao {
//    private DBHelper helper;
    private SQLiteDatabase db;
    private DBUtils dbUtils;

    public ContentDao(Context ctx) {
//        helper=DBHelper.getDbHelper(ctx);
//        //通过helper打开数据库
//        db=helper.getDb();
        dbUtils=new DBUtils(ctx);
        db=dbUtils.getDatabase();
    }

    /**
     * 从网上读取的数据添加到数据库中
     * @param list
     */
    public void addAll(List<Content> list) {
        List<Content> templist=new ArrayList<>();
        for(int k=0;k<list.size();k++) {
            //在本地查询出的结果
            Content content=findContentByLink(list.get(k).getLink());
            if (content == null) {
                templist.add(list.get(k));//templist存储的都是本地不存在的
            }
        }
        //开启事务
        db.beginTransaction();
        //数据添加前先判断本地是否存在
        for(int i=0;i<templist.size();i++) {
            Content content=templist.get(i);
            db.execSQL("insert into content values(null,?,?,?,?,?,?,?,?)",
                    new Object[]{content.getLink(),content.getTitle(),
                            content.getPubDate(),content.getChannelName(),content.getDesc(),
                            content.getSource(),content.getReply(),content.getHtml()});
            for(int j=0;j<content.getImageurls().size();j++) {
                ImageUrl imageUrl=content.getImageurls().get(j);
                db.execSQL("insert into imageurl values(null,?,?,?,?)",new Object[]{content.getLink(),
                        imageUrl.getUrl(),imageUrl.getHeight(),imageUrl.getWidth()});
            }

        }
        db.setTransactionSuccessful();
        db.endTransaction();
    }

    /**
     * 通过link查询新闻
     * @param link
     * @return
     */
    public Content findContentByLink(String link) {
        Content content=null;
        Cursor cursor=db.rawQuery("select * from content where link=?",new String[]{link});
        while (cursor.moveToNext()) {
            content=new Content();
            content.setTitle(cursor.getString(cursor.getColumnIndex("title")));
            content.setChannelName(cursor.getString(cursor.getColumnIndex("channelName")));
            content.setPubDate(cursor.getString(cursor.getColumnIndex("pubDate")));
            content.setContent(cursor.getString(cursor.getColumnIndex("content")));
            content.setDesc(cursor.getString(cursor.getColumnIndex("desc")));
            content.setSource(cursor.getString(cursor.getColumnIndex("source")));
            content.setReply(cursor.getInt(cursor.getColumnIndex("reply")));
            content.setHtml(cursor.getString(cursor.getColumnIndex("html")));
        }
        return content;
    }

    /**
     * 查询新闻列表，通过新闻分类
     * @param channel
     * @return
     */
    public List<Content> findContentByChannel(Channel channel) {
        List<Content> list=new ArrayList<>();
        Cursor cursor=db.rawQuery("select * from content where channelName=? limit 20 offset 0 ",
                new String[]{channel.getName()});
        while (cursor.moveToNext()) {
            Content content=new Content();
            content.setLink(cursor.getString(cursor.getColumnIndex("link")));

            //查询新闻对应的图片
            Cursor imgCursor=db.rawQuery("select * from imageurl where link=?",
                    new String[]{content.getLink()});
            List<ImageUrl> imageUrls=new ArrayList<>();
            while (imgCursor.moveToNext()) {
                ImageUrl imageUrl=new ImageUrl();
                imageUrl.setLink(content.getLink());
                imageUrl.setUrl(imgCursor.getString(imgCursor.getColumnIndex("url")));
                imageUrl.setHeight(imgCursor.getInt(imgCursor.getColumnIndex("height")));
                imageUrl.setWidth(imgCursor.getInt(imgCursor.getColumnIndex("width")));
                imageUrls.add(imageUrl);
            }

            content.setChannelName(cursor.getString(cursor.getColumnIndex("channelName")));
            content.setDesc(cursor.getString(cursor.getColumnIndex("desc")));
            content.setTitle(cursor.getString(cursor.getColumnIndex("title")));
            content.setReply(cursor.getInt(cursor.getColumnIndex("reply")));
            content.setHtml(cursor.getString(cursor.getColumnIndex("html")));
            content.setPubDate(cursor.getString(cursor.getColumnIndex("pubDate")));
            content.setSource(cursor.getString(cursor.getColumnIndex("source")));
            content.setImageurls(imageUrls);
            list.add(content);
        }
        return list;
    }

    public void closeDB() {
        if (db.isOpen()) {
            db.close();
        }
    }
}
